package com.bytech.user.biz.controller.blogUser;

import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.bouncycastle.math.ec.rfc7748.X448.Friend;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bytech.common.annotation.UserAgent;
import com.bytech.common.biz.controller.base.BasicController;
import com.bytech.common.biz.entity.base.BaseResult;
import com.bytech.common.biz.entity.blogUser.BlogUserAgent;
import com.bytech.common.biz.entity.friendChain.FriendChain;
import com.bytech.common.biz.entity.friendChain.FriendChainDto;
import com.bytech.common.biz.entity.friendChain.FriendChainQuery;
import com.bytech.common.biz.enums.friendChain.FriendChainAuditStatusEnum;
import com.bytech.common.utils.StringUtils;
import com.bytech.user.biz.service.friendChain.FriendChainService;

/**
 * @Author Zhao
 * @Create 2021/10/8 16:09
 */
@RestController
@RequestMapping("/friendChain")
public class FriendChainController extends BasicController {

    @Resource
    private FriendChainService friendChainService;

    @GetMapping("/list")
    public BaseResult<List<FriendChain>> getFriendChainList() {
        LambdaQueryWrapper<FriendChain> friendChainQuery = new LambdaQueryWrapper<>();
        friendChainQuery.eq(FriendChain::getAuditStatus, FriendChainAuditStatusEnum.PASS.getValue());
        return BaseResult.<List<FriendChain>>success().data(friendChainService.list(friendChainQuery)).build();
    }

    @PostMapping("/pageChainByQuery")
    public BaseResult<Page<FriendChainDto>> pageFriendChainByQuery(@RequestBody FriendChainQuery<FriendChain> chainQuery) {
        return BaseResult.<Page<FriendChainDto>>success().data(friendChainService.pageChainByQuery(chainQuery)).build();
    }

    @PostMapping("/apply")
    public BaseResult<String> applyChain(FriendChain chain, HttpServletRequest request, HttpServletResponse response, @UserAgent BlogUserAgent blogUserAgent) {
        return friendChainService.applyFriendChain(chain, request, response, blogUserAgent);
    }

    @PostMapping("/delete")
    public BaseResult<String> deleteChain(@RequestParam String id) {
        friendChainService.removeById(id);
        return BaseResult.<String>success().msg("删除成功").build();
    }

    @PostMapping("/modifyChain")
    public BaseResult<String> modifyChain(@RequestBody FriendChain chain) {
        if (StringUtils.isBlank(chain.getId())) {
            return BaseResult.<String>error("友链不存在").build();
        }
        chain.setModifyTime(new Date());
        friendChainService.updateById(chain);
        return BaseResult.<String>success().msg("修改成功").build();
    }
}
